var mygrid;
var dataurl="fileflow/file/fileflow!datalist";
$(function() {
	mygrid = $("#list").jqGrid( {
		url : dataurl,
		datatype : "json",
		mtype : 'GET',
		colNames : [ '文件名', '发布日期', '类型', '文件大小', '下载', '转发', '删除' ],
		colModel : [ {
			name : 'name',
			index : 'name',
			width : 300,
			formatter : nameFormater
		}, {
			name : 'updateDate',
			index : 'updateDate',
			width : 100
		}, {
			name : 'extision',
			index : 'extision',
			width : 30
		}, {
			name : 'size',
			index : 'size',
			width : 60,
			formatter : sizeFormatter
		}, {
			name : 'vid',
			index : 'id',
			width : 30,
			formatter : viewFormatter
		}, {
			name : 'sid',
			index : 'id',
			width : 30,
			formatter : sendFormatter
		}, {
			name : 'rid',
			index : 'id',
			width : 30,
			formatter : removeFormatter
		} ],
		pager : '#pager',
		rowNum : 10,
		rowList : [ 10, 20, 30 ],
		sortname : 'updateDate',
		sortorder : 'desc',
		viewrecords : true,
		caption : '公司文件',
		width : '750',
		height : '300',
		jsonReader : {
			repeatitems : false,
			id : "0"
		}
	});

	$("#cdate").datepicker({ dateFormat: 'yy-mm-dd' });
	$("#searchBtn").click(function(){
		search();
	});
       
});// end of document ready
function nameFormater(cellvalue, options, row) {
	var now = new Date();
	var update = row['updateDate'];
	var udate = new Date(update);
	var rds = row['readereds'];
	var eid = row['id'];
	var url = "fileflow/file/fileflow!view"+"?bean.id="+eid+"&user.username="+loginuser+"&bean.extision="+row['extision'];
	var link = $("<a></a>");
	link.attr("href", "javascript:doOpenWindow('" + url + "');");
    //link.attr("target","_blank");
	link.html("<b>"+cellvalue+"</b>");
	return $("<span></span>").append(link).html();

}
function sizeFormatter(cellvalue, options, rowObject) {
	var size = (cellvalue / 1000) + "K";
	return size;
}

function viewFormatter(cellvalue, options, rowObject) {
	var eid = rowObject['id'];
	var url = 'fileflow/file/fileflow!download?bean.id='+eid;
	var link = $("<a target='_blank'></a>");
	link.attr("href", url);
    //link.attr("target","_blank");
	link.html("<b>下载</b>");
	return $("<span></span>").append(link).html();
}

function sendFormatter(cellvalue, options, rowObject) {
	var eid = rowObject['id'];
	
	var url = 'fileflow/file/fileflow!send';
	var link = $("<a></a>");
	link.attr("href", "javascript:openUserDlg('" + eid + "');");
	link.html("<b>转发</b>");
	return $("<span></span>").append(link).html();
}

function removeFormatter(cellvalue, options, row) {
	var manager = row['manager'];
	if (loginuser != manager.username) {
		return "-";
	}
	var eid = row['id'];
	var url = 'fileflow/file/fileflow!remove';
	var link = $("<a></a>");
	link.attr("href", "javascript:removeFiles('" + url + "','" + eid
			+ "');");
	link.html("<b style='color:red;font-weight:bold'>删除</b>");
	return $("<span></span>").append(link).html();
}
function openUserDlg(id) {
	
	var url = "fileflow/file/fileflow!checkperm?bean.id="+id;
    doGet(url);
}
function removeFiles(url, eid) {
    $.ajax({
    	url:'fileflow/file/fileflow!removes',
    	data:"beans[0].id="+eid,
    	success:function(){
    		mygrid.trigger("reloadGrid");
    	}
    });	
	
}
function doOpenWindow(url){
	window.open(url);
}
function search(){
	var ps = $("#fileForm").serialize();
	//alert(ps);
	var objs = ps.split("&");
	var pms = {};
	for(var i=0;i<objs.length;i++){
		var oa = objs[i].split("=");
		pms[decodeURIComponent(oa[0])]=decodeURIComponent(oa[1]);
	}
	mygrid.jqGrid('setGridParam',{url:dataurl,postData:pms}).trigger("reloadGrid");
}